import axios from 'axios';
import storageService from '../service/storageService';

//
// 发送form
//
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 1000 * 5,
  headers: {
    Authorization: `Bearer ${storageService.get(storageService.USER_TOKEN)}`,
    'Content-Type': 'multipart/form-data', // 'application/x-www-form-urlencoded',
  },
});

// Add a request interceptor
service.interceptors.request.use((config) => {
  // Do something before request is sent
  Object.assign(config.headers, { Authorization: `Bearer ${storageService.get(storageService.USER_TOKEN)}` });
  return config;
}, (error) => Promise.reject(error)); // Do something with request error

//
// 发送json
//
const serviceJson = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 1000 * 5,
  headers: {
    Authorization: `Bearer ${storageService.get(storageService.USER_TOKEN)}`,
    'Content-Type': 'application/json',
  },
});

// Add a request interceptor
serviceJson.interceptors.request.use((config) => {
  // Do something before request is sent
  Object.assign(config.headers, { Authorization: `Bearer ${storageService.get(storageService.USER_TOKEN)}` });
  return config;
}, (error) => Promise.reject(error)); // Do something with request error

export {
  service,
  serviceJson,
};
